package fzdy.core.viewResolver;


import org.apache.velocity.context.Context;
import org.apache.velocity.tools.ToolManager;
import org.apache.velocity.tools.view.ViewToolContext;
import org.springframework.web.servlet.view.velocity.VelocityToolboxView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/27 0027.
 */
public class BdVelocityView extends VelocityToolboxView {
    public BdVelocityView() {
    }

    protected Context createVelocityContext(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ViewToolContext ctx = new ViewToolContext(this.getVelocityEngine(), request, response, this.getServletContext());
        ctx.putAll(model);
        if(this.getToolboxConfigLocation() != null) {
            ToolManager tm = new ToolManager();
            tm.setVelocityEngine(this.getVelocityEngine());
            tm.configure(this.getServletContext().getRealPath(this.getToolboxConfigLocation()));
            if(tm.getToolboxFactory().hasTools("request")) {
                ctx.addToolbox(tm.getToolboxFactory().createToolbox("request"));
            }

            if(tm.getToolboxFactory().hasTools("application")) {
                ctx.addToolbox(tm.getToolboxFactory().createToolbox("application"));
            }

            if(tm.getToolboxFactory().hasTools("session")) {
                ctx.addToolbox(tm.getToolboxFactory().createToolbox("session"));
            }
        }

        return ctx;
    }
}